package com.jeesite.modules.ic.service;

import java.util.List;

import com.jeesite.modules.ic.channel.CardDataService;
import com.jeesite.modules.ic.entity.IcChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.ic.entity.IcCard;
import com.jeesite.modules.ic.dao.IcCardDao;

/**
 * 流量卡Service
 * @author 李寒冰
 * @version 2024-06-19
 */
@Service
public class IcCardService extends CrudService<IcCardDao, IcCard> {

	@Autowired
	private UserDataService userDataService;

	@Autowired
	@Qualifier("card_data_1")
	private CardDataService zemiCardInfoService;

	@Autowired
	private IcChannelService icChannelService;

	@Autowired
	ApplicationContext applicationContext;

	public void getCardInfo(IcCard icCard){
		String channalId = icCard.getChannalId();
		if(channalId!=null){
			CardDataService cardDataService= applicationContext.getBean("card_data_"+channalId,CardDataService.class);
			cardDataService.getCardInfoByIccid(icCard);
		}else{
			List<IcChannel> list = icChannelService.findList(new IcChannel());
			for (int i = 0; i < list.size(); i++) {
				IcChannel icChannel = list.get(i);
				CardDataService cardDataService= applicationContext.getBean("card_data_"+icChannel.getId(),CardDataService.class);
				cardDataService.getCardInfoByIccid(icCard);
				if(icCard.getChannalId()!=null){
					break;
				}
			}
		}
	}
	
	/**
	 * 获取单条数据
	 * @param icCard
	 * @return
	 */
	@Override
	public IcCard get(IcCard icCard) {
		return super.get(icCard);
	}
	
	/**
	 * 查询分页数据
	 * @param icCard 查询条件
	 * @param icCard page 分页对象
	 * @return
	 */
	@Override
	public Page<IcCard> findPage(IcCard icCard) {
		return super.findPage(icCard);
	}

	public IcCard getOneCard(IcCard icCard){
		List<IcCard> list = findList(icCard);
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}

	public IcCard getOneCard(String iccid){
		IcCard icCard = new IcCard();
		icCard.setIccid(iccid);
		List<IcCard> list = findList(icCard);
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}

	/**
	 * 查询列表数据
	 * @param icCard
	 * @return
	 */
	@Override
	public List<IcCard> findList(IcCard icCard) {
		return super.findList(icCard);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param icCard
	 */
	@Override
	@Transactional
	public void save(IcCard icCard) {
		boolean isNewRecord = icCard.getIsNewRecord();
		super.save(icCard);

		if(isNewRecord){
			userDataService.synUser(icCard.getUserId());
		}
	}
	
	/**
	 * 更新状态
	 * @param icCard
	 */
	@Override
	@Transactional
	public void updateStatus(IcCard icCard) {
		super.updateStatus(icCard);
	}
	
	/**
	 * 删除数据
	 * @param icCard
	 */
	@Override
	@Transactional
	public void delete(IcCard icCard) {
		super.delete(icCard);
		userDataService.synUser(icCard.getUserId());
	}
	
}